package com.jlastu.znb.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;

public class OrdersActivity extends AppCompatActivity {
    private ImageView mBack;
    private TextView mBigTextView1;
    private TextView mBigTextView2;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mTextView4;
    private TextView mTextView5;
    private TextView mTextView6;
    private String orderId;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        // 初始化视图
        mBack = findViewById(R.id.back);
        mBigTextView1 = findViewById(R.id.orders_big_title1);
        mBigTextView2 = findViewById(R.id.orders_big_title2);
        mTextView1 = findViewById(R.id.orders_title1);
        mTextView2 = findViewById(R.id.orders_title2);
        mTextView3 = findViewById(R.id.orders_title3);
        mTextView4 = findViewById(R.id.orders_title4);
        mTextView5 = findViewById(R.id.orders_title5);
        mTextView6 = findViewById(R.id.orders_title6);

        // 设置返回按钮点击事件
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        // 获取Intent中的标志
        Intent intent = getIntent();
        String defaultTab = intent.getStringExtra("SELECT_DEFAULT_TAB");
        String defaultTitle = intent.getStringExtra("SELECT_DEFAULT_TITLE");

        // 根据SELECT_DEFAULT_TAB设置大标题的默认选中状态
        if ("orders_big_title2".equals(defaultTab)) {
            setBigTabSelected(mBigTextView2);
        } else {
            setBigTabSelected(mBigTextView1);
        }

        // 根据SELECT_DEFAULT_TITLE设置小标题的默认选中状态
        if ("orders_title2".equals(defaultTitle)) {
            setTabSelected(mTextView2);
        } else if ("orders_title3".equals(defaultTitle)) {
            setTabSelected(mTextView3);
        } else if ("orders_title4".equals(defaultTitle)) {
            setTabSelected(mTextView4);
        } else if ("orders_title5".equals(defaultTitle)) {
            setTabSelected(mTextView5);
        } else if ("orders_title6".equals(defaultTitle)) {
            setTabSelected(mTextView6);
        } else {
            // 默认选中第一个小标题
            setTabSelected(mTextView1);
        }

        // 设置标题点击事件
        setClickListenersForBigTabs();
        setClickListenersForTabs();

        // 从SharedPreferences获取订单ID
        orderId = getOrderIdFromSharedPreferences();

        // 根据订单ID获取订单详情并更新UI
        refreshUIWithOrderDetails(orderId);
    }

    // 设置大标题点击事件
    private void setClickListenersForBigTabs() {
        mBigTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBigTabSelected(mBigTextView1);
            }
        });
        mBigTextView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBigTabSelected(mBigTextView2);
            }
        });
    }

    // 设置小标题点击事件
    private void setClickListenersForTabs() {
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView1);
            }
        });
        mTextView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView2);
            }
        });
        mTextView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView3);
            }
        });
        mTextView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView4);
            }
        });
        mTextView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView5);
            }
        });
        mTextView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView6);
            }
        });
    }

    private void setBigTabSelected(TextView selectedTab) {
        // 重置所有标签为未选中状态
        mBigTextView1.setSelected(false);
        mBigTextView2.setSelected(false);
        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }
    private void setTabSelected(TextView selectedTab) {
        // 重置所有标签为未选中状态
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);
        mTextView4.setSelected(false);
        mTextView5.setSelected(false);
        mTextView6.setSelected(false);
        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }
    private String getOrderIdFromSharedPreferences() {
        SharedPreferences preferences = getSharedPreferences("OrderInfo", MODE_PRIVATE);
        return preferences.getString("current_order_id", "");
    }

    private void refreshUIWithOrderDetails(String orderId) {

    }
}
